package ext.tianma.forms.processors;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import wt.change2.ChangeException2;
import wt.change2.ChangeHelper2;
import wt.change2.ChangeOrder2;
import wt.change2.ChangeRequestIfc;
import wt.change2.WTChangeOrder2;
import wt.change2.WTChangeRequest2;
import wt.fc.QueryResult;
import wt.lifecycle.State;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.util.WTException;
import wt.vc.Iterated;
import wt.vc.views.View;
import wt.vc.views.ViewException;
import wt.vc.views.ViewHelper;

import com.ptc.core.components.beans.ObjectBean;
import com.ptc.core.components.forms.FormResult;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.windchill.enterprise.change2.forms.processors.CreateChangeNoticeFormProcessor;

import ext.com.core.CoreUtil;
import ext.com.iba.IBAUtil;
import ext.tianma.change.processors.ModifiedFinishedGoodUtil;
import ext.tianma.part.forms.TMTypeUtil;

public class CusCreateChangeNoticeFormProcessor extends CreateChangeNoticeFormProcessor {
	//实施状态
	private static final String IMPLEMENTATION = "IMPLEMENTATION";

	@Override
	public FormResult postProcess(NmCommandBean numcommandbean, List<ObjectBean> list)
			throws WTException {

		FormResult formResult = super.postProcess(numcommandbean, list);
		Iterator<ObjectBean> iterator = list.iterator();
		while ( iterator.hasNext() ) {
			ObjectBean objbean = iterator.next();
			Object obj =objbean.getObject();
			if( ( obj !=  null ) && ( obj instanceof Iterated ) ){
				Iterated iterated  =( Iterated ) obj;
				System.out.println( "Debug   postProcess:"+iterated );
				
				if( iterated instanceof WTChangeOrder2 ){
					WTChangeOrder2 ecn = ( WTChangeOrder2 )iterated;
					System.out.println( "Debug   ecn : " + ecn.getDisplayIdentity() );
					//消费
					boolean isConsumptions = TMTypeUtil.isATypeObject( ecn , ".Consumptions" );
					//先进技术
					boolean isAdvTechCN = TMTypeUtil.isATypeObject( ecn , ".AdvTechCN" );
					//专显
					boolean isDisplays = TMTypeUtil.isATypeObject( ecn , ".Displays" );
					
					if( isAdvTechCN || isConsumptions || isDisplays ){
						if( objbean.getObject() != null  && ( objbean.getObject() instanceof ChangeOrder2 ) ){
							checkRequest( objbean );
						}
					}
				}
				
				setModifiedFinshedGoodList(iterated);
			}	
		}		
		return formResult;
		
	}
	
	/**
	 * 控制消费类变更、专显类变更和新技术类变更必须在变更请求上来创建
	 * @param objbean
	 * @throws WTException
	 */
	private void checkRequest( ObjectBean objbean ) throws WTException {
		if( objbean == null ){
			return;
		}
		NmOid localNmOid = objbean.getActionOid();
		
		if( ( localNmOid != null ) && ( localNmOid.isA( WTChangeRequest2.class ) ) ){
			WTChangeRequest2 ecr = ( WTChangeRequest2 )localNmOid.getRefObject();
			
			State state = ecr.getLifeCycleState();
			if( state != null ){
				String ecrState = state.toString();
				System.out.println( "Debug   ecrState : " + ecrState );
				if( !ecrState.equals( IMPLEMENTATION ) ){
					throw new WTException( "当前变更请求没有生效无法创建变更公告" );
				}
			}
		}else{
			throw new WTException( "消费类变更、专显类变更和新技术类变更必须在变更请求上来创建" );
		}
	}

	@Override
	public FormResult doOperation(NmCommandBean nmcommandBean, List<ObjectBean> list)
			throws WTException {

		FormResult formresult = super.doOperation(nmcommandBean , list);
		Iterator<ObjectBean> iterator = list.iterator();
		
		while (iterator.hasNext()) {
			ObjectBean objbean = iterator.next();
			Object obj =objbean.getObject();
			if( ( obj !=  null ) && ( obj instanceof Iterated ) ){
				Iterated iterated  =( Iterated ) obj;
				
				ChangeNumberUtil.updateChangeItemNumber(iterated);	
				
			}
		}
		return formresult;
	}
	
	private String setModifiedFinshedGoodList( Iterated iterated ){
		System.out.println( "Debug setModifiedFinshedGoodList..." );
		String modifiedFinshedGoodList = "" ;
		
		if(iterated != null && iterated instanceof WTChangeOrder2 ){
			System.out.println( "Debug   (WTChangeOrder2) iterated"+ (WTChangeOrder2) iterated);
			modifiedFinshedGoodList = getModifiedFinshedGoodList( (WTChangeOrder2) iterated ) ;
			System.out.println( "Debug   modifiedFinshedGoodList:"+modifiedFinshedGoodList );
			try {
				
				IBAUtil.forceSetIBAValue( (WTChangeOrder2) iterated , "modifiedFinishedGood", modifiedFinshedGoodList);
				
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
		
		return modifiedFinshedGoodList ;
	}
	
	
	
	private String getModifiedFinshedGoodList( WTChangeOrder2 ecn ){
		String modifiedFinshedGoodList = "" ;
		
		List<WTPart> partList = getAllECREndItem( ecn );
		
		Iterator<WTPart> partListIte = partList.iterator() ;
		
		while( partListIte.hasNext() ){
			WTPart part = partListIte.next() ;
			
			modifiedFinshedGoodList = modifiedFinshedGoodList + ModifiedFinishedGoodUtil.getPartInfo(part) ;
		}
		
		return modifiedFinshedGoodList ;
	}
	
	private List<WTPart> getAllECREndItem( WTChangeOrder2 ecn ){
		List<WTPart> partList = new ArrayList<WTPart>() ;
		
		List<WTPartMaster> partMasterList = getAllECREndItemMaster( ecn ) ;
		
		try {
			View[] allViews = ViewHelper.service.getAllViews();
			
			if( allViews != null ){
				Iterator<WTPartMaster> partMasterIte = partMasterList.iterator() ;
				
				while( partMasterIte.hasNext() ){
					WTPartMaster partMaster = partMasterIte.next() ;
					
					addAllViewParts( partMaster , allViews , partList ) ;
					
				}
			}
		} catch (ViewException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
		
		return partList ;
	}
	
	private void addAllViewParts(WTPartMaster partMaster ,View[] allViews,List<WTPart> partList ) throws WTException{
		if(partList == null){
			partList = new ArrayList<WTPart>() ;
		}
		
		if(partMaster == null || allViews == null ){
			return ;
		}
		
		for(int i=0 ; i < allViews.length ; i++ ){
			if( allViews[i] != null && ! allViews[i].equals("")){
				String viewName = allViews[i].getName() ;
				System.out.println( "Debug     addAllViewParts："+viewName + "   Master："+partMaster );
				WTPart part = CoreUtil.getWTPartByMasterAndView( partMaster , viewName ) ;
				
				if( part != null && part.getViewName().equals( viewName )){
					partList.add(part) ;
				}
			}
		}
	}
	
	private List<WTPartMaster> getAllECREndItemMaster( WTChangeOrder2 ecn ){
		List<WTPartMaster> partMasterList = new ArrayList<WTPartMaster>() ;
		
		try {
			QueryResult ecrQr = ChangeHelper2.service.getLatestChangeRequest(ecn);
			System.out.println( "Debug   ecrQr:"+ecrQr.size() );
			
			if( ecrQr != null ){
				while( ecrQr.hasMoreElements() ){
					Object ecrObj = ecrQr.nextElement() ;
					System.out.println( "Debug    ecrQr.hasMoreElements()..." );
					if( ecrObj != null && ecrObj instanceof ChangeRequestIfc ){
						ChangeRequestIfc ecrLfc = (ChangeRequestIfc) ecrObj;
						
						QueryResult endItemQr =  ChangeHelper2.service.getSubjectProducts( ecrLfc );
						
						if( endItemQr != null ){
							while( endItemQr.hasMoreElements() ){
								Object endItemObj = endItemQr.nextElement() ;
								
								if( endItemObj != null && endItemObj instanceof WTPartMaster ){
									WTPartMaster partMaster = ( WTPartMaster ) endItemObj ;
									
									if(! partMasterList.contains(partMaster) ){
										partMasterList.add(partMaster) ;
									}
								}
							}
						}
					}
				}
			}
		} catch (ChangeException2 e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
		System.out.println( "Debug   getAllECREndItemMaster:"+ partMasterList);
		return partMasterList ;
	}	
}
